import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Callable<Integer> sumtask = () -> {
            int sum = 0;
            for (int i = 1; i <= 100; i++) {
                sum += i;
            }
            return sum;
        };
        FutureTask<Integer> sumFuture = new FutureTask<>(sumtask);
        Thread t1 = new Thread(sumFuture);
        t1.start();

        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("a");
                }
            }
        };
        t2.start();
        Thread t3 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("b");
                }
            }
        };
        t3.start();
        int sumResult = sumFuture.get();
        System.out.println("Sum: " + sumResult);
        t1.join();
        t2.join();
        t3.join();
    }
}
